package com.huawei.hwid.core.d.a;

import android.content.Context;
import android.text.TextUtils;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.d.g;
import com.huawei.hwid.core.encrypt.c;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class b implements a {
    public static synchronized String a(Context context, String str) {
        FileOutputStream fileOutputStream;
        FileInputStream fileInputStream;
        String str2;
        Throwable th;
        Throwable th2;
        FileOutputStream fileOutputStream2 = null;
        synchronized (b.class) {
            FileInputStream openFileInput;
            try {
                Properties properties = new Properties();
                if (new File(context.getFilesDir().getPath() + "/" + "settings.properties").exists()) {
                    fileOutputStream = null;
                } else {
                    e.e("PropertiesGrade", "getOldProperties file not exit", true);
                    fileOutputStream = context.openFileOutput("settings.properties", 0);
                }
                try {
                    openFileInput = context.openFileInput("settings.properties");
                    if (openFileInput != null) {
                        try {
                            properties.load(openFileInput);
                        } catch (FileNotFoundException e) {
                            fileOutputStream2 = fileOutputStream;
                            fileInputStream = openFileInput;
                            try {
                                e.d("PropertiesGrade", "Can not find the file settings.properties", true);
                                if (fileOutputStream2 != null) {
                                    try {
                                        fileOutputStream2.close();
                                    } catch (IOException e2) {
                                        e.d("PropertiesGrade", "IOException ", true);
                                    }
                                }
                                if (fileInputStream != null) {
                                    try {
                                        fileInputStream.close();
                                    } catch (IOException e3) {
                                        e.d("PropertiesGrade", "IOException  ", true);
                                    }
                                }
                                str2 = "";
                                return str2;
                            } catch (Throwable th3) {
                                th = th3;
                                openFileInput = fileInputStream;
                                fileOutputStream = fileOutputStream2;
                                th2 = th;
                                if (fileOutputStream != null) {
                                    try {
                                        fileOutputStream.close();
                                    } catch (IOException e4) {
                                        e.d("PropertiesGrade", "IOException ", true);
                                    }
                                }
                                if (openFileInput != null) {
                                    try {
                                        openFileInput.close();
                                    } catch (IOException e5) {
                                        e.d("PropertiesGrade", "IOException  ", true);
                                    }
                                }
                                throw th2;
                            }
                        } catch (IOException e6) {
                            fileOutputStream2 = fileOutputStream;
                            try {
                                e.d("PropertiesGrade", "IOException  ", true);
                                if (fileOutputStream2 != null) {
                                    try {
                                        fileOutputStream2.close();
                                    } catch (IOException e7) {
                                        e.d("PropertiesGrade", "IOException ", true);
                                    }
                                }
                                if (openFileInput != null) {
                                    try {
                                        openFileInput.close();
                                    } catch (IOException e8) {
                                        e.d("PropertiesGrade", "IOException  ", true);
                                    }
                                }
                                str2 = "";
                                return str2;
                            } catch (Throwable th4) {
                                th = th4;
                                fileOutputStream = fileOutputStream2;
                                th2 = th;
                                if (fileOutputStream != null) {
                                    fileOutputStream.close();
                                }
                                if (openFileInput != null) {
                                    openFileInput.close();
                                }
                                throw th2;
                            }
                        } catch (Throwable th5) {
                            th2 = th5;
                            if (fileOutputStream != null) {
                                fileOutputStream.close();
                            }
                            if (openFileInput != null) {
                                openFileInput.close();
                            }
                            throw th2;
                        }
                    }
                    e.b("PropertiesGrade", "getOldProperties inStream is null", true);
                    str2 = properties.getProperty(str);
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e9) {
                            e.d("PropertiesGrade", "IOException ", true);
                        }
                    }
                    if (openFileInput != null) {
                        try {
                            openFileInput.close();
                        } catch (IOException e10) {
                            e.d("PropertiesGrade", "IOException  ", true);
                        }
                    }
                } catch (FileNotFoundException e11) {
                    FileOutputStream fileOutputStream3 = fileOutputStream;
                    fileInputStream = null;
                    fileOutputStream2 = fileOutputStream3;
                    e.d("PropertiesGrade", "Can not find the file settings.properties", true);
                    if (fileOutputStream2 != null) {
                        fileOutputStream2.close();
                    }
                    if (fileInputStream != null) {
                        fileInputStream.close();
                    }
                    str2 = "";
                    return str2;
                } catch (IOException e12) {
                    openFileInput = null;
                    fileOutputStream2 = fileOutputStream;
                    e.d("PropertiesGrade", "IOException  ", true);
                    if (fileOutputStream2 != null) {
                        fileOutputStream2.close();
                    }
                    if (openFileInput != null) {
                        openFileInput.close();
                    }
                    str2 = "";
                    return str2;
                } catch (Throwable th32) {
                    th = th32;
                    openFileInput = null;
                    th2 = th;
                    if (fileOutputStream != null) {
                        fileOutputStream.close();
                    }
                    if (openFileInput != null) {
                        openFileInput.close();
                    }
                    throw th2;
                }
            } catch (FileNotFoundException e13) {
                fileInputStream = null;
                e.d("PropertiesGrade", "Can not find the file settings.properties", true);
                if (fileOutputStream2 != null) {
                    fileOutputStream2.close();
                }
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                str2 = "";
                return str2;
            } catch (IOException e14) {
                openFileInput = null;
                e.d("PropertiesGrade", "IOException  ", true);
                if (fileOutputStream2 != null) {
                    fileOutputStream2.close();
                }
                if (openFileInput != null) {
                    openFileInput.close();
                }
                str2 = "";
                return str2;
            } catch (Throwable th42) {
                openFileInput = null;
                th2 = th42;
                fileOutputStream = null;
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
                if (openFileInput != null) {
                    openFileInput.close();
                }
                throw th2;
            }
        }
        return str2;
    }

    public void a(Context context, int i, int i2) {
        if (i >= i2) {
            e.d("PropertiesGrade", "newVersion is less then oldVersion, onUpgrade error", true);
            return;
        }
        e.b("PropertiesGrade", "update settings.properties when version update", true);
        a(context, i);
    }

    private void a(Context context, int i) {
        e.b("PropertiesGrade", "begin update curName in settings.properties", true);
        String a = a(context, "curName");
        g.a(context, new String[]{"curName"});
        if (TextUtils.isEmpty(a)) {
            e.b("PropertiesGrade", "curName is null in settings.properties", true);
            return;
        }
        if (com.huawei.hwid.core.d.b.a(context, "isSDKAccountDataEncrypted", false)) {
            g.a(context, new String[]{"isSDKAccountDataEncrypted"});
            a = c.a(context, a);
        } else if (i < 3) {
            a = c.c(context, a);
        }
        if (TextUtils.isEmpty(a)) {
            e.b("PropertiesGrade", "curName ecb decrypt error", true);
            return;
        }
        e.b("PropertiesGrade", "update curName in settings.properties", true);
        g.a(context, "curName", a);
    }
}
